package ru.usu.gv.gui.frames;

import ru.usu.gv.core.GraphCanvas;
import ru.usu.gv.core.I18nService;
import ru.usu.gv.gui.GUIUtilities;
import ru.usu.gv.styles.StyleFacade;
import ru.usu.gv.uicomponents.IUIEdgeType;
import ru.usu.gv.uicomponents.UIEdge;
import ru.usu.gv.uicomponents.UIEdgeTypeRegistry;
import ru.usu.gv.uicomponents.UIGraph;
import ru.usu.gv.uicomponents.UIVertex;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.LineBorder;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Map;

/**
 * @author spupyrev
 * 07.11.2009
 */
public class HighlightDynamicEdgeFrame extends JDialog
{
	public HighlightDynamicEdgeFrame()
	{
		setTitle(I18nService.get("layout.dynamic.choose_edges"));
		initComponents();
	}

	private void initComponents()
	{
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

		Map<String, JCheckBox> checkBoxes = createHighlightCheckboxes();
		JPanel wrapComponentByPanel = GUIUtilities.wrapComponentByPanel(new JLabel(I18nService.get("layout.dynamic.choose_edges")));
		wrapComponentByPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		mainPanel.add(wrapComponentByPanel);
		mainPanel.add(new JSeparator());
		mainPanel.add(createCheckboxesPanel(checkBoxes));
		mainPanel.add(new JSeparator());

		JPanel applyPanel = createOkCancelPanel(checkBoxes);

		getContentPane().add(mainPanel, BorderLayout.NORTH);
		getContentPane().add(applyPanel, BorderLayout.SOUTH);

		getRootPane().setBorder(new LineBorder(Color.blue, 3));
		setModal(true);
		setUndecorated(true);
		pack();
		GUIUtilities.centralizeFrame(this);
	}

	private JPanel createOkCancelPanel(final Map<String, JCheckBox> checkBoxes)
	{
		JPanel panel = new JPanel();

		JButton okButton = new JButton(new AbstractAction(I18nService.get("layout.dynamic.apply"))
		{
			public void actionPerformed(ActionEvent e)
			{
				unhighlightAll();

				for (String label : checkBoxes.keySet())
					if (checkBoxes.get(label).isSelected())
						highlightEdge(label);

				HighlightDynamicEdgeFrame.this.dispose();
			}
		});
		panel.add(okButton);

		JButton closeButton = new JButton(new AbstractAction(I18nService.get("layout.dynamic.cancel"))
		{
			public void actionPerformed(ActionEvent e)
			{
				HighlightDynamicEdgeFrame.this.dispose();
			}
		});
		panel.add(closeButton);

		return panel;
	}

	private JPanel createCheckboxesPanel(final Map<String, JCheckBox> checkBoxes)
	{
		JPanel panel = new JPanel();
		int rows = Math.min(checkBoxes.size() + 2, 15);
		int cols = (checkBoxes.size() + 2 + rows - 1) / rows;
		panel.setLayout(new GridLayout(rows, cols, 2, 2));
		for (JCheckBox checkBox : checkBoxes.values())
		{
			panel.add(checkBox);
		}

		JButton checkAllButton = new JButton(new AbstractAction(I18nService.get("layout.dynamic.select_all"))
		{
			public void actionPerformed(ActionEvent e)
			{
				for (JCheckBox checkBox : checkBoxes.values())
					checkBox.setSelected(true);
			}
		});
		checkAllButton.setFont(new Font(Font.DIALOG, Font.BOLD, 11));
		panel.add(GUIUtilities.wrapComponentByPanel(checkAllButton));

		JButton checkNoneButton = new JButton(new AbstractAction(I18nService.get("layout.dynamic.deselect_all"))
		{
			public void actionPerformed(ActionEvent e)
			{
				for (JCheckBox checkBox : checkBoxes.values())
					checkBox.setSelected(false);
			}
		});
		checkNoneButton.setFont(new Font(Font.DIALOG, Font.BOLD, 11));
		panel.add(GUIUtilities.wrapComponentByPanel(checkNoneButton));

		return panel;
	}

	private void unhighlightAll()
	{
		UIGraph uiGraph = GraphCanvas.getInstance().getCurrentGraph();
		for (UIEdge e : uiGraph.getEdges())
		{
			e.changeEdgeType(UIEdgeTypeRegistry.findEdgeType(StyleFacade.getEdgeType()));
			e.getViewItem().setColor(StyleFacade.getEdgeColor());
		}
	}

	private void highlightEdge(String label)
	{
		UIGraph uiGraph = GraphCanvas.getInstance().getCurrentGraph();
		for (UIEdge e : uiGraph.getEdges())
			if (e.getV1().getLabel().equals(label) && e.getV2().getLabel().equals(label))
			{
				IUIEdgeType tubeType = UIEdgeTypeRegistry.findEdgeType("tube");
				e.changeEdgeType(tubeType);
				e.getViewItem().setColor(Color.red);
			}
	}

	private Map<String, JCheckBox> createHighlightCheckboxes()
	{
		UIGraph uiGraph = GraphCanvas.getInstance().getCurrentGraph();
		Map<String, JCheckBox> result = new HashMap();
		Map<String, Integer> labels = new HashMap();
		for (UIVertex v : uiGraph.getVertexes())
		{
			if (labels.containsKey(v.getLabel()))
			{
				labels.put(v.getLabel(), labels.get(v.getLabel()) + 1);
			}
			else
			{
				labels.put(v.getLabel(), 1);
			}
		}
		for (UIVertex v : uiGraph.getVertexes())
		{
			int cnt = labels.get(v.getLabel());
			if (cnt > 1)
			{
				result.put(v.getLabel(), new JCheckBox(v.getLabel() + " (" + cnt + ")"));
			}
		}
		return result;
	}

}
